var util = '$$$util$$$' in this ? this['$$$util$$$'] : require('./libra.util');
var ModelRequest = util.using('model.promise.request');
var Controller = util.using('controller');

var m = ModelRequest.create(function(v, k){
	switch(k){
		case 'text':
		case 'error':
			return v || null;
		case 'status':
			if(util.isValidNumber(v)) return v;
			return 0;
		default:
			return v;
	}
});

var temp = '<div>\
<h3>{city} <span>{weather}</span></h3>\
<h4><label><img src="http://m.weather.com.cn/img/{img1}" alt="img1"></label>{temp1}</h4>\
<h4><label><img src="http://m.weather.com.cn/img/{img2}" alt="img2"></label>{temp2}</h4>\
<h5>{ptime}</h5>\
</div>\
<button onclick="request()">Update</button>\
';

var c = Controller.create(function(target, val, oldVal, model, targetId){
	if(!val || val.status <= 0){
		console.log('Loading...');
		// target.innerHTML = 'Loading...';
	} else if(!val.error && val.text) {
		val = JSON.parse(val.text);
		setTimeout(function(){
			// target.innerHTML = util.format(temp, val.weatherinfo);
			console.log(util.format(temp, val.weatherinfo));
		}, 1000);
	}
});

// c.bind(document.querySelector('#view'));

m.bind(c);

// var url = 'http://127.0.0.1/git/libra/weather.php';
var url = 'http://www.weather.com.cn/data/cityinfo/101010100.html';
function request(){
	m.reset().request({ url: url });
}
request();